package net.hmapp.app.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import net.hmapp.app.activity.R;
import net.hmapp.app.adapter.ChannelAdapter;
import net.hmapp.app.api.ApiService;
import net.hmapp.app.interfaces.ResponseCallback;
import net.hmapp.app.viewmodel.Banner;
import net.hmapp.app.viewmodel.Channel;
import net.hmapp.app.viewmodel.HomeData;
import net.hmapp.app.viewmodel.ResponseData;

import java.util.Collections;
import java.util.List;

public class HomeFragment extends Fragment {


    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;

    private  com.youth.banner.Banner viewBanner;
    private GridView gv_channel;
    private List<Channel> channels;
    private List<Banner> bannerData;


    public HomeFragment() {
        // Required empty public constructor
    }
//视图组件
    private void initView(View view){
        viewBanner = view.findViewById(R.id.banner);
        gv_channel = view.findViewById(R.id.gv_channel);
    }

    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.fragment_home, container, false);
//       初始化视图
        initView(view);

        ApiService.GetHome(new ResponseCallback<ResponseData<HomeData>>() {
            @Override
            public void onSuccess(ResponseData<HomeData> callback) {
                bannerData = callback.getData().getBanner();
                channels = callback.getData().getChannel();


                viewBanner.setAdapter(new BannerImageAdapter<Banner>(bannerData) {
                            @Override
                            public void onBindView(BannerImageHolder holder, Banner data, int position, int size) {
                                // 图片加载自己实现
                                Glide.with(holder.itemView)
                                        .load(data.getUrl())
                                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                                        .into(holder.imageView);
                            }
                        })
                        .addBannerLifecycleObserver(HomeFragment.this) // 添加生命周期观察者
                        .setIndicator(new CircleIndicator(view.getContext()));


                gv_channel.setAdapter(new ChannelAdapter(view.getContext(),channels));
            }

        });

        return view;
    }
}